/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/fsd.h"

/**
 * @brief Opens a directory.
 *
 * This function opens the directory 'name' and returns a directory stream.
 *
 * @param name The name of the directory to open.
 * @return TDIR* The directory stream, or NULL on error.
 */
TDIR *Sopendir(const char *name) {
    TDIR *dir = (TDIR *)syscall(SYS_mmap, 0, sizeof(TDIR), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

    if (!dir)
        return NULL;

    dir->fd = syscall(SYS_open, name, O_RDONLY | O_DIRECTORY);
    if (dir->fd < 0) {
        syscall(SYS_munmap, dir, sizeof(TDIR));
        return NULL;
    }

    dir->pos = 0;
    return dir;
}
